/**
 * Created by Administrator on 2017/5/19.
 */
angular.module('service.shopmenu.user', ['ngResource'])

    .service("ShopMenuService",['$resource',function($resource) {
        var productions = productionList;
        this.getProduction = function(){
            return productions;
        };
        this.add = function(id){
            for(var i=0;i<productions.length;i++){
                var goods = productions[i].goodsList;
                for(var j=0;j<goods.length;j++){
                    var good = goods[j];
                    if (id == good.id) {
                        good.num++;
                        break;
                    }
                }
            }
        };
        this.cut = function(id){
            for (var i = 0; i < productions.length; i++) {
                var goods = productions[i].goodsList;
                for (var j = 0; j < goods.length; j++) {
                    var good = goods[j];
                    if (id == good.id) {
                        if(good.num>0){
                            good.num--;
                        }
                        break;
                    }
                }
            }
        };
        this.totalprice= function(money) {
            var allprice = 0;
            var tranmoney = 0;
            var hasDDB = false;
            for(var i=0;i<productions.length;i++){
                var goods = productions[i].goodsList;
                for(var j=0;j<goods.length;j++) {
                    var good = goods[j];
                    if(good.num>0){
                        if(good.transport){
                            tranmoney+=good.num*money;
                            hasDDB = true;
                        }
                        if(good.firstDiscount){
                            if(good.num>0){
                                allprice+=(good.num-1)*good.price+0.01
                            }
                        }else{
                            allprice+=good.price*good.num;
                        }
                    }
                }
            }
            if(!hasDDB){
                tranmoney = money;
            }
            if(allprice>0){
                allprice+=tranmoney;
            }
           /* return allprice;*/
           return {allprice:allprice,tranmoney:tranmoney};
        };

        //获取购买商品列表
        this.allgoodlist=function(money){
            var order = {};
            var allgood=[];
            for(var i=0;i<productions.length;i++){
                var goods = productions[i].goodsList;
                for(var j=0;j<goods.length;j++) {
                    var good = goods[j];
                    if(good.num>0){
                        good.totalfee = good.num*good.price;
                        allgood.push(good)
                    }
                }
            }
            order.goodsList = allgood;
            var price = this.totalprice(money);
            order.transFee = price.tranmoney;
            order.totalFee = price.allprice;
            return order;
        }
    }])